/**
 * Copyright (C) 2002-2006 WUZEWEN. All rights reserved.
 * WUZEWEN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */

package com.kingcore.director.setting.ui;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.sql.SQLException;
import java.util.HashMap;

import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.tree.DefaultMutableTreeNode;

import wzw.beans.CodeNameBean;
import wzw.swing.tree.NullIconTreeCellRenderer;
import wzw.swing.tree.PlainTree;
import wzw.swing.tree.PlainTreeModel;
import wzw.util.ComponentUtil;

import com.kingcore.director.base.ui.DirectorDialog;
import com.kingcore.director.main.Main;
import com.kingcore.director.main.MainFrame;

/**
 * <p>the Preference Window for director application.</p>
 * @author	WUZEWEN on 2006-05-16
 * @version	1.0
 * @see		Object#equals(java.lang.Object)
 * @see		Object#hashCode()
 * @see		HashMap
 * @since	JDK5
 */

public class PreferenceDialog extends DirectorDialog 
	implements ComponentListener, ActionListener, MouseListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JPanel mainPanel = null;
	private JSplitPane mainSplitPane = null;
	private JSplitPane topSplitPane = null;
	private JScrollPane leftScrollPane = null;
	private PlainTree tree = null;
	private JPanel bottomPanel = null;
	private JButton btnOk = null;
	private JButton btnCnl = null;
	private MainFrame mainFrame;
	private GeneralPanel pgeneralPane;
	private NetConfigPanel pnetPane;
	private JScrollPane rightScrollPane = null;
	private FileBackupPanel pfilebackupPane=null;
	private FileCopyPanel pfilecopyPane=null;
	private int tree_file_rowNumber=3;

	/**
	 * This method initializes jSplitPane	
	 * 	
	 * @return javax.swing.JSplitPane	
	 */
	private JSplitPane getMainSplitPane() {
		if (mainSplitPane == null) {
			mainSplitPane = new JSplitPane();
			mainSplitPane.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT);
			mainSplitPane.setDividerSize(2);
			mainSplitPane.setTopComponent(getTopSplitPane());
			mainSplitPane.setBottomComponent(getBottomPanel());
			mainSplitPane.setDividerLocation(410);
		}
		return mainSplitPane;
	}

	/**
	 * This method initializes jSplitPane1	
	 * 	
	 * @return javax.swing.JSplitPane	
	 */
	private JSplitPane getTopSplitPane() {
		if (topSplitPane == null) {
			topSplitPane = new JSplitPane();
			topSplitPane.setDividerSize(5);
			topSplitPane.setLeftComponent(getLeftScrollPane());
			topSplitPane.setRightComponent(getRightScrollPane());
			topSplitPane.setDividerLocation(160);
		}
		return topSplitPane;
	}

	/**
	 * This method initializes jScrollPane	
	 * 	
	 * @return javax.swing.JScrollPane	
	 */
	private JScrollPane getLeftScrollPane() {
		if (leftScrollPane == null) {
			leftScrollPane = new JScrollPane();
			leftScrollPane.setViewportView(getTree());
		}
		return leftScrollPane;
	}

	/**
	 * 
	 * 	
	 * @return javax.swing.JTree	
	 */
	private PlainTree getTree() {
		if (tree == null) {

		    DefaultMutableTreeNode root = 
		      new DefaultMutableTreeNode(new CodeNameBean("","选项设置") );
		    
		    DefaultMutableTreeNode general = 
			      new DefaultMutableTreeNode( new CodeNameBean("00","常规"));
		    DefaultMutableTreeNode net = 
			      new DefaultMutableTreeNode( new CodeNameBean("01","网络"));
		    DefaultMutableTreeNode file = 
			      new DefaultMutableTreeNode( new CodeNameBean("02","文件"));

			    DefaultMutableTreeNode copy = 
				      new DefaultMutableTreeNode( new CodeNameBean("0200","文件复制"));
			    DefaultMutableTreeNode backup = 
			      new DefaultMutableTreeNode( new CodeNameBean("0201","文件备份"));
		    
//		    DefaultMutableTreeNode ord = 
//		      new DefaultMutableTreeNode( new CodeName("0000","普通提醒"));
//		    DefaultMutableTreeNode imp = 
//		      new DefaultMutableTreeNode( new CodeName("0001","重要提醒"));
//		    DefaultMutableTreeNode bri = 
//		      new DefaultMutableTreeNode( new CodeName("0002","生日"));
//		    DefaultMutableTreeNode ply = 
//		      new DefaultMutableTreeNode( new CodeName("0003","日程、计划"));
//			DefaultMutableTreeNode ait = 
//			      new DefaultMutableTreeNode( new CodeName("0004","约会"));
//			DefaultMutableTreeNode els = 
//			      new DefaultMutableTreeNode( new CodeName("0005","其它"));
//			
//
//		    DefaultMutableTreeNode gbg = 
//			      new DefaultMutableTreeNode( new CodeName("99","垃圾箱"));

		    
			PlainTreeModel dtm = new PlainTreeModel(root);
		    dtm.insertNodeInto(general, root, 0);
		    dtm.insertNodeInto(net, root, 1);
		    dtm.insertNodeInto(file, root, 2);
		    
		    dtm.insertNodeInto(copy, file, 0);
		    dtm.insertNodeInto(backup, file, 1);
		    
//		    dtm.insertNodeInto(ord, dir, 0);
//		    dtm.insertNodeInto(imp, dir, 1);
//		    dtm.insertNodeInto(bri, dir, 2);
//		    dtm.insertNodeInto(ply, dir,3);
//		    dtm.insertNodeInto(ait, dir,4);
//		    dtm.insertNodeInto(els, dir,5);

			tree = new PlainTree( dtm );

		    tree.setRootVisible(true);	//true
		    tree.setCellRenderer( new NullIconTreeCellRenderer() );
			tree.addMouseListener( this );
			//System.out.println( tree.getRowCount() );
			tree.expandRow(tree_file_rowNumber);
			tree.setSelectionRow( 1 );
		}
		return tree;
	}

	/**
	 * This method initializes jPanel	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getBottomPanel() {
		if (bottomPanel == null) {
			bottomPanel = new JPanel();
			bottomPanel.setLayout(null);
			bottomPanel.add(getBtnOk(), null);
			bottomPanel.add(getBtnCnl(), null);
		}
		return bottomPanel;
	}

	/**
	 * This method initializes jButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getBtnOk() {
		if (btnOk == null) {
			btnOk = new JButton();
			btnOk.setBounds(new java.awt.Rectangle(401,7,82,28));
			btnOk.setText("保存(S)");
			btnOk.setMnemonic('S');
			btnOk.addActionListener(this);
		}
		return btnOk;
	}

	/**
	 * This method initializes jButton1	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getBtnCnl() {
		if (btnCnl == null) {
			btnCnl = new JButton();
			btnCnl.setBounds(new java.awt.Rectangle(511,7,82,28));
			btnCnl.setText("关闭(C)");
			btnCnl.setMnemonic('C');
			btnCnl.addActionListener(this);
		}
		return btnCnl;
	}

	/**
	 * This method initializes jScrollPane	
	 * 	
	 * @return javax.swing.JScrollPane	
	 */
	private JScrollPane getRightScrollPane() {
		if (rightScrollPane == null) {
			rightScrollPane = new JScrollPane();
		}
		return rightScrollPane;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		PreferenceDialog pfr = new PreferenceDialog(null);
		pfr.setVisible( true );

	}

//	/**
//	 * This is the default constructor
//	 */
//	public PreferenceDialog() {
//		this(null);
//	}

	public PreferenceDialog(MainFrame mf) {
		super( mf, true);
		this.mainFrame = mf ;
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(623, 485);
		this.setTitle("首选项");
		this.setContentPane(getMainPanel());
		this.addComponentListener( this );
		
		//默认打开界面
		rightScrollPane.setViewportView(getPgeneralPane());
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getMainPanel() {
		if (mainPanel == null) {
			mainPanel = new JPanel();
			mainPanel.setLayout(new BorderLayout());
			mainPanel.add(getMainSplitPane(), java.awt.BorderLayout.CENTER);
		}
		return mainPanel;
	}

	
	private void execTreeSingleClick(MouseEvent e){
		String code ;
		code = ComponentUtil.getSelectedTreeNodeCode( getTree() );
		//System.out.println("code="+code);
		if(code==null)
			return;
		if(code.equals("00")){		//general
			rightScrollPane.setViewportView(getPgeneralPane());
			
		} else if (code.equals("01")){	//net
			rightScrollPane.setViewportView(getPnetPane());
			
		} else if (code.equals("0200")){ //file copy
			rightScrollPane.setViewportView( getPfilecopyPane() );
			
		} else if (code.equals("0201")){ //file backup
			rightScrollPane.setViewportView(getPfilebackupPane());
			
		}
	}
	private JPanel getPgeneralPane() {
		if (pgeneralPane==null) {
			
			pgeneralPane = new GeneralPanel(this.mainFrame);
		}
		return pgeneralPane;
	}
	private JPanel getPnetPane() {
		if (pnetPane==null) {
			
			pnetPane = new NetConfigPanel();
		}
		return pnetPane;
	}
	private JPanel getPfilebackupPane() {
		if (pfilebackupPane==null) {
			
			pfilebackupPane = new FileBackupPanel( this );
		}
		return pfilebackupPane;
	}
	private JPanel getPfilecopyPane() {
		if (pfilecopyPane==null) {
			
			pfilecopyPane = new FileCopyPanel();
		}
		return pfilecopyPane;
	}

	
	private void execTreeDoubleClick(MouseEvent e){
		
	}
	
	/*********************************************************
	 * all below method is to implements ComponentListener.
	 *********************************************************/
	
	/**
	 * must use intValue to translate double value to int value. 
	 */
	public void componentResized(ComponentEvent e) {
		Object src = e.getSource();
		if(src==this){
			//System.out.println( mainSplitPane.getSize().getHeight() );
			mainSplitPane.setDividerLocation( new Double(mainSplitPane.getSize().getHeight()).intValue()-50 );
		}
		
	}

	public void componentMoved(ComponentEvent e) {
		// TODO Auto-generated method stub
		
	}

	public void componentShown(ComponentEvent e) {
		// TODO Auto-generated method stub
		
	}

	public void componentHidden(ComponentEvent e) {
		// TODO Auto-generated method stub
		
	}

	/*********************************************************
	 * all below method is to implements ComponentListener.
	 *********************************************************/
	
	public void actionPerformed(ActionEvent e) {
		Object src = e.getSource();
		if ( src==btnOk){		// 保存所有信息
			
			try {
				if(pgeneralPane !=null){
					pgeneralPane.saveData();
				}
				if(pnetPane !=null){
					pnetPane.saveData();
				}
				if(pfilebackupPane !=null){
					pfilebackupPane.saveData();
				}
				if(pfilebackupPane !=null){
					pfilebackupPane.saveData();
				}
				if(pfilecopyPane !=null){
					pfilecopyPane.saveData();
				}
				//JOptionPane.showMessageDialog(this, "");
				if(JOptionPane.showConfirmDialog(this, "保存成功！部分设置的修改需要重载应用才生效，现在就重新载入吗?"
						, "提示", JOptionPane.OK_CANCEL_OPTION)
							==JOptionPane.OK_OPTION){
					Main.reload();
					return;
				}
				
			} catch (SQLException e1) {
				JOptionPane.showMessageDialog(this,e1.getMessage(),"数据库操作失败",JOptionPane.ERROR_MESSAGE);
				e1.printStackTrace();
				
			} catch (Exception e1) {
				JOptionPane.showMessageDialog(this,e1.getMessage(),"执行操作失败",JOptionPane.ERROR_MESSAGE);
				e1.printStackTrace();
			}
			
		} else if( src==btnCnl){
			dispose();
		}
		
	}
	
	/*********************************************************
	 * all below method is to implements MouseListener.
	 *********************************************************/
	
	public void mouseClicked(MouseEvent e) {
		Object src = e.getSource();
		if(src==getTree()){
			if(!e.isPopupTrigger() && e.getClickCount()==1) {		//单击tree
				//System.out.println("tree one click!");
				execTreeSingleClick(e);
				
			} else if(!e.isPopupTrigger() && e.getClickCount()==2) {//双击tree
				//System.out.println("tree double click!");
				execTreeDoubleClick(e);
				
			}
		}
	}

	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

}  //  @jve:decl-index=0:visual-constraint="10,10"
